<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

session_start();

// 0. include primary files
$toolbox = new Toolbox();
$db_access = new DbAccessor();
$loginManager = new LoginManager();
$access_manager = new AccessManager();

$start = $toolbox->getmicrotime();
include_once("sys/config.inc");

function __autoload($class_name) {
	if(preg_match("/^.+Page$/", $class_name)) {
		require_once("sys/pages/$class_name.inc");
	}
	else {
		require_once("sys/$class_name.inc");
	}
}

global $CONFIG, $ACCESSLIST, $no_menu;

$db_access->connect();

$ACCESSLIST = array();

// 1. Start session
if ($_GET['action'] == "logout") {
	$loginManager->logout();
	unset($_GET['action']);
} else if ($_SESSION['user_id'] == "" && ($_GET['action'] == "login" || $_COOKIE[$CONFIG['cookie_name']] != "")) {
	$loginManager->login();
	if ($_GET['action'] == "login") {
		unset($_GET['action']);
	}
}

// 2. Derive page location from URL
$pageFactory = new PageFactory();
$page = $pageFactory->get();
$toolbox->fetch_moderators($page, $page->directory_tree[$page->dir_depth - 1]['dir_id']);

// 3. Generate menu
if (!$no_menu) {
	$menu = new Menu();
	$page->menu = $menu->generate();
}

// 4. Check access
$access_manager->generate_accesslist($page);
$access_manager->check_access("VIEW");
$page->pageType->page_inc_check_access($page);
if ($access_manager->has_access("ADMIN")) {
	$toolbox->add_action($page, 'admin', 'url', '.admin');
}

// 5. Execute page logic
if ($_GET['action']) {
	$page->pageType->page_inc_execute_action($page, $_GET['action']);
}
$page->pageType->page_inc_execute($page);

// 6. Check mail & messages
$mailer = new Mailer($page);
if ($_SESSION['user_id']) {
	$mailer->fetch_alerts($page);
	$mailer->fetch_mail($page);
}

// 6.5 who's online !
if ($_SESSION['member']['pref_last_clicks']) {
	$page->last_clicks = $toolbox->get_last_clicks();
}
if ($_SESSION['user_id']) {
	$toolbox->click();
}

// 7. Dress up content
// load skin
include_once("sys/skins/{$CONFIG['default_skin']}/main.inc");

// default printing
print_header($page, $no_menu);
if (!$no_menu)
	print_actions($page, $page->actions);
print_title($page, $page->content['title']);
print_alerts($page, $page->alerter);
if (!$no_menu) {
	print_mailform($page, $mailer->mail_form(), $_POST['mailing'] or $_GET['mailto']);
}

// apply skin
$page->pageType->page_inc_dress($page);

// default printing
print_footer($page, (round($toolbox->getmicrotime()-$start, 4)), $no_menu);

finish($page);
$db_access->close();

?>
